﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LandandSurvey.BLL;

namespace LandandSurvey
{
    public partial class AddBookPlan : System.Web.UI.Page
    {
        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                if ( !HasPermission() )
                {
                    Response.Redirect( "ErrorPermission.aspx?returnurl=AddBookPlan.aspx" );
                }
                if ( !IsPostBack )
                {
                    Manager manager = new Manager();
                    ddlSurveyor.DataSource = manager.GetSurveyors( false );
                    ddlSurveyor.DataTextField = "Name";
                    ddlSurveyor.DataBind();

                    ddlWard.DataSource = manager.GetCountys( false );
                    ddlWard.DataTextField = "Ward";
                    ddlWard.DataBind();
                }
            }
        }

        public bool HasPermission()
        {
            if ( Session[ "user.loggedin" ] == null )
            {
                return false;
            }
            if ( Session[ "user.loggedin" ].ToString() == "true" )
            {
                landsdbEntities container = new landsdbEntities();
                string username = Session[ "user.name" ].ToString();
                AppUser user = ( from i in container.AppUsers
                                 where i.UserName == username
                                 select i ).ToList().FirstOrDefault();
                if ( user != null )
                {
                    if ( user.UploadFiles == true )
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        private void ClearForm()
        {
            txtAcreage.Text = "";
            txtCTFolio.Text = "";
            txtCTVolume.Text = "";
            txtDateSignedDirector.Text = "";
            txtDateSignedSurveyor.Text = "";
            txtLSNumber.Text = "";
            txtName.Text = "";
            txtBookNumber.Text = "";
            txtFolio.Text = "";
            txtRemarks.Text = "";
            txtRPOFolio.Text = "";
            txtRPOVolume.Text = "";
            txtSurveoyOrderNo.Text = "";
            txtWardRefCassini.Text = "";
            txtWardRefCassini.Text = "";
            txtWardRefUTM.Text = "";
            txtDeedNumber.Text = "";
        }

        protected void btnAdd_Click( object sender, EventArgs e )
        {
            BookPlan otherplan = new BookPlan();
            landsdbEntities context = new landsdbEntities();

            otherplan.Acreage = txtAcreage.Text;
            otherplan.CTFolio = txtCTFolio.Text;
            otherplan.CTVolume = txtCTVolume.Text;
            otherplan.DateSignedDirector = txtDateSignedDirector.Text;
            otherplan.DateSignedSurveyor = txtDateSignedSurveyor.Text;
            otherplan.LandSNumber = txtLSNumber.Text;
            otherplan.Name = txtName.Text;
            otherplan.BookNo = txtBookNumber.Text;
            otherplan.Remarks = txtRemarks.Text;
            otherplan.RPOFolio = txtRPOFolio.Text;
            otherplan.RPOVolume = txtRPOVolume.Text;
            otherplan.Surveyor = ddlSurveyor.Text;
            otherplan.SurveyOrderNumber = txtSurveoyOrderNo.Text;
            otherplan.WardRefCassini = txtWardRefCassini.Text;
            otherplan.Ward = ddlWard.Text;
            otherplan.WardRefCassini = txtWardRefCassini.Text;
            otherplan.WardRefUTM = txtWardRefUTM.Text;
            otherplan.DeedNumber = txtDeedNumber.Text;
            otherplan.Folio = txtFolio.Text;

            context.BookPlans.AddObject( otherplan );
            try
            {
                context.SaveChanges();
                ClearForm();
                lblMessage.InnerText = "Plan added successfully";
            }
            catch
            {
                lblMessage.InnerText = "Error adding Plan";
            }
        }
    }
}